<!DOCTYPE html>
<html>
  
  <head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
	
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="/static/xadmin/css/font.css">
	<link rel="stylesheet" href="/static/xadmin/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script src="/static/xadmin/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/static/xadmin/js/xadmin.js"></script>
	
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  
  <body>
    <div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a href="">管理员管理</a>
		<a href="">角色管理</a>
        <a>
          <cite>用户列表</cite></a>
      </span>
      <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
    </div>
    <div class="x-body">
    <div class="demoTable">
	    <button class="layui-btn"  data-type="AddUserToRole"><i class="layui-icon"></i>添加用户到角色</button>
        <div class="layui-inline">
            <input class="layui-input" name="Username" placeholder="输入用户名" id="SearchUsername" autocomplete="off">
        </div>
        <button class="layui-btn" data-type="search">搜索</button>
    </div>
    <table class="layui-table" lay-data="{url:'/rbac/user/index',headers: {'X-Requested-With':'XMLHttpRequest'},page:true,limit:10,limits:[10, 20,50],id:'UserList'}" lay-filter="UserList">
      <thead>
        <tr> 
          <th lay-data="{type:'checkbox'}"></th>
          <th lay-data="{field:'Id',sort: true}">ID</th>
		  <th lay-data="{field:'Username',sort: true}">用户名</th>
          <th lay-data="{field:'Nickname',sort: true}">昵称</th>
          <th lay-data="{field:'Email',sort: true}">Email</th>
          <th lay-data="{field:'Lastlogintime',sort: true}">上次登录</th>
          <th lay-data="{field:'Createtime',sort: true}">创建时间</th>
		  <th lay-data="{field:'Status',sort: true}">状态</th>
          <th lay-data="{field:'Remark',sort: true}">备注</th>
        </tr>
      </thead>
    </table>	
	
    <table class="layui-table" lay-data="{url:'/rbac/role/RoleToUserList' + location.search,headers: {'X-Requested-With':'XMLHttpRequest'},page:true,toolbar: '#toolbarHead2',limit:10,limits:[10, 20,50],id:'RoleToUserList'}" lay-filter="RoleToUserList">
      <thead>
        <tr> 
          <th lay-data="{type:'checkbox'}"></th>
          <th lay-data="{field:'Id',sort: true}">ID</th>
		  <th lay-data="{field:'Username',sort: true}">用户名</th>
          <th lay-data="{field:'Nickname',sort: true}">昵称</th>
          <th lay-data="{field:'Email',sort: true}">Email</th>
          <th lay-data="{field:'Lastlogintime',sort: true}">上次登录</th>
          <th lay-data="{field:'Createtime',sort: true}">创建时间</th>
		  <th lay-data="{field:'Status',sort: true}">状态</th>
          <th lay-data="{field:'Remark',sort: true}">备注</th>
        </tr>
      </thead>
    </table>

    </div>

<script type="text/html" id="toolbarHead2">
  <div class="layui-btn-container">
	<button class="layui-btn layui-btn-danger" lay-event="DelUserInRole"><i class="layui-icon"></i>从角色中删除用户</button>
  </div>
</script>		
    <script>
    layui.use(['form','table','laydate'], function(){
        var laydate = layui.laydate;
		var table = layui.table;
        var form=layui.form;

    var $ = layui.$, active = {
        search: function(){
            var demoReload = $('#SearchUsername');
            table.reload('UserList', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                ,where: {
                    key: {
                        Username: demoReload.val()
                    }
                }
            });
        }
        ,AddUserToRole: function(){
			var checkStatus = table.checkStatus('UserList')
                ,data = checkStatus.data;
	        var list='';
		    var roleId = location.search.replace("?Id=", "");
	        for (var i in data){
			    list+=data[i].Id+',';
            }
	        vpnidstr = list.substr(0,list.length-1);
			var jsonstr = '{"Id":"' + roleId + '","ids":"'+vpnidstr+'"}'
	        if(vpnidstr.length > 0){
                $.ajax({
                    url: "/rbac/role/AddRoleToUser",
                    type: "post",
                    data: JSON.parse(jsonstr),
                    dataType: "json",
                    success: function(resp) {
                                if(resp.status){
									table.reload('RoleToUserList', {
                                           url: '/rbac/role/RoleToUserList' + location.search
                                    });									
                                    layer.msg('添加成功', {icon:1});
                                }else{
                                     layer.msg('添加失败',{icon:2});
                                }
                    }
                });
	        }else{
	            console.log("未选中");
	        }	  
        }				
	};	

    //头工具栏事件
    table.on('toolbar(RoleToUserList)', function(obj){
    switch(obj.event){
      case 'DelUserInRole':	
		    var checkStatus = table.checkStatus('RoleToUserList')
            ,data = checkStatus.data;
	        var list='';
		    var roleId = location.search.replace("?Id=", "");
	        for (var i in data){
			    list+=data[i].Id+',';
            }
	        vpnidstr = list.substr(0,list.length-1);
			var jsonstr = '{"Id":"' + roleId + '","ids":"'+vpnidstr+'"}'
			console.log(jsonstr);		
            layer.confirm('确定删除?', {icon: 3, title:'删除提示',yes: function(index){
			$.ajax({
			    url: "/rbac/role/DelRoleToUser",
			    type: "post",
			    data: JSON.parse(jsonstr),
			    dataType: "json",
			    success: function(resp) {
						if(resp.status){
							layer.msg('删除成功', {icon: 1});
							    table.reload('RoleToUserList', {
                                    url: '/rbac/role/RoleToUserList' + location.search
                                });							
							//obj.del();
						}else{
							layer.msg('删除失败',{icon:2});
						}
				}		
            });		  	  
            },
            cancel: function(index, layero){ 
                layer.close(index);
		        console.log("不操作");
            }
		    });			   			
        break; 
    };
    });

   $('.demoTable .layui-btn').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });	
});  
    </script>
  </body>

</html>